/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.start.model;

import business.service.MongoDbFactory;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.star.common.NewsCateData;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.log4j.Logger;
import org.bson.types.ObjectId;

/**
 *
 * @author Administrator
 */
public class NewsCateModel {
    
    private static final String NEWSCATE_COLLECTION = "news_category";
    private static final Logger logger = Logger.getLogger(NewsCateModel.class);
    private static final Lock createLock_ = new ReentrantLock();
    private static NewsCateModel _instance = null;
    
    public static NewsCateModel getInstance() {
        if (_instance == null) {
            createLock_.lock();
            try {
                if (_instance == null) {
                    _instance = new NewsCateModel();

                }
            } finally {
                createLock_.unlock();
            }
        }
        return _instance;
    }

    public NewsCateModel() {
        try {
        } catch (Exception ex) {
            logger.error("NewsCateModel: " + ex);
        }
    }
    
    
    
    public String addCate(NewsCateData newsCateData) {
        String id = "";
        try {
            BasicDBObject basicDBObject = new BasicDBObject("name", newsCateData.getName())   
                    .append("created", System.currentTimeMillis())
                    .append("code", newsCateData.getCode())
                    .append("cate_pa", newsCateData.getId_cate_pa());
            
            DB db = MongoDbFactory.getConnection();
            DBCollection coll = db.getCollection(NEWSCATE_COLLECTION);
            coll.insert(basicDBObject);
            
            ObjectId objectId = (ObjectId)basicDBObject.get( "_id" );
            id = objectId.toString();
        } catch (Exception ex) {
            logger.error("NewsCateModel.addCate: " + ex.getMessage(), ex);
        }
        return id;
    }
    
    
    public boolean editCate(NewsCateData newsCateData) {
        try {     
            DB db = MongoDbFactory.getConnection();
            DBCollection coll = db.getCollection(NEWSCATE_COLLECTION);
                        
            BasicDBObject basicDBObject = new BasicDBObject("name", newsCateData.getName())
                    .append("code", newsCateData.getCode())
                    .append("cate_pa", newsCateData.getId_cate_pa());
            BasicDBObject updateDBObject = new BasicDBObject("$set", basicDBObject);
            BasicDBObject searchDBQuery = new BasicDBObject("_id", new ObjectId(newsCateData.getId()));
            coll.update(searchDBQuery, updateDBObject);
            return true;
        } catch (Exception ex) {
            logger.error("NewsCateModel.editCate: " + ex.getMessage(), ex);
        } 
        return false;
    }
    
    
    public boolean editLstCate(List<NewsCateData> lstCate) {
        try {     
            DB db = MongoDbFactory.getConnection();
            DBCollection coll = db.getCollection(NEWSCATE_COLLECTION);
                        
            for(NewsCateData cateData : lstCate){
                BasicDBObject basicDBObject = new BasicDBObject("name", cateData.getName())
                    .append("code", cateData.getCode())
                    .append("cate_pa", "");
                BasicDBObject updateDBObject = new BasicDBObject("$set", basicDBObject);
                BasicDBObject searchDBQuery = new BasicDBObject("_id", new ObjectId(cateData.getId()));
                coll.update(searchDBQuery, updateDBObject);
            }            
                       
            return true;
        } catch (Exception ex) {
            logger.error("NewsCateModel.editLstCate: " + ex.getMessage(), ex);
        } 
        return false;
    }
    
    
    
    public boolean deleteCate(String id) {
        try {
            DB db = MongoDbFactory.getConnection();
            DBCollection coll = db.getCollection(NEWSCATE_COLLECTION);
            coll.remove(new BasicDBObject("_id", new ObjectId(id)));
            
            return true;
        } catch (Exception ex) {
            logger.error("NewsCateModel.deleteCate: " + ex.getMessage(), ex);
        }
        return false;
    }
    
    
    public List<NewsCateData> getCate(int start, int limit) {
        List<NewsCateData> lstCate = null;
        try {
            BasicDBObject basicDBObject = new BasicDBObject("_id", -1);
            DB db = MongoDbFactory.getConnection();
            DBCollection coll = db.getCollection(NEWSCATE_COLLECTION);
            DBCursor cursor = coll.find().sort(basicDBObject);
            if (limit != -1) {
                cursor = cursor.skip(start).limit(limit);
            }
            lstCate = new ArrayList<>();
            while (cursor.hasNext()) {
                DBObject dbObject = cursor.next();
                String id = ((ObjectId)dbObject.get("_id")).toString();
                String name = (String) dbObject.get("name");
                String code = (String) dbObject.get("code");
                String id_cate_pa = (String) dbObject.get("cate_pa");
                long created = (long) dbObject.get("created");
                
                NewsCateData newsCateData = new NewsCateData(id, name, created, id_cate_pa, code);
                lstCate.add(newsCateData);
            }
        } catch (Exception ex) {
            logger.error("NewsCateModel.getCate: " + ex.getMessage(), ex);
        } 
        return lstCate;
    }
    
    
    
    public List<NewsCateData> getCateParent(int start, int limit) {
        List<NewsCateData> lstCate = null;
        try {
            BasicDBObject basicDBObject = new BasicDBObject("name", -1);
            BasicDBObject query = new BasicDBObject();
            query.append("cate_pa", "");
            DB db = MongoDbFactory.getConnection();
            DBCollection coll = db.getCollection(NEWSCATE_COLLECTION);
            DBCursor cursor = coll.find(query).sort(basicDBObject);
            if (limit != -1) {
                cursor = cursor.skip(start).limit(limit);
            }
            lstCate = new ArrayList<>();
            while (cursor.hasNext()) {
                DBObject dbObject = cursor.next();
                String id = ((ObjectId)dbObject.get("_id")).toString();
                String name = (String) dbObject.get("name");
                String code = (String) dbObject.get("code");
                String id_cate_pa = (String) dbObject.get("cate_pa");
                long created = (long) dbObject.get("created");
                
                NewsCateData newsCateData = new NewsCateData(id, name, created, id_cate_pa, code);
                lstCate.add(newsCateData);
            }
        } catch (Exception ex) {
            logger.error("NewsCateModel.getCateParent: " + ex.getMessage(), ex);
        } 
        return lstCate;
    }
    
    
    public List<NewsCateData> getCateSubByParent(String id_Pa) {
        List<NewsCateData> lstCate = null;
        try {
            BasicDBObject basicDBObject = new BasicDBObject("name", -1);
            BasicDBObject query = new BasicDBObject();
            query.append("cate_pa", id_Pa);
            DB db = MongoDbFactory.getConnection();
            DBCollection coll = db.getCollection(NEWSCATE_COLLECTION);
            DBCursor cursor = coll.find(query).sort(basicDBObject);
            
            lstCate = new ArrayList<>();
            while (cursor.hasNext()) {
                DBObject dbObject = cursor.next();
                String id = ((ObjectId)dbObject.get("_id")).toString();
                String name = (String) dbObject.get("name");
                String code = (String) dbObject.get("code");
                String id_cate_pa = (String) dbObject.get("cate_pa");
                long created = (long) dbObject.get("created");
                
                NewsCateData newsCateData = new NewsCateData(id, name, created, id_cate_pa, code);
                lstCate.add(newsCateData);
            }
        } catch (Exception ex) {
            logger.error("NewsCateModel.getCateParent: " + ex.getMessage(), ex);
        } 
        return lstCate;
    }
    
    
    public NewsCateData getNewsCateById(String id) {
        NewsCateData newsCateData = null;
        try {
            BasicDBObject basicDBObject = new BasicDBObject("_id", new ObjectId(id));
            
            DB db = MongoDbFactory.getConnection();
            DBCollection coll = db.getCollection(NEWSCATE_COLLECTION);
            DBCursor cursor = coll.find(basicDBObject);
            if(cursor.hasNext()) {
                DBObject dbObject = cursor.next();
                String name = (String) dbObject.get("name");
                String code = (String) dbObject.get("code");
                String id_cate_pa = (String) dbObject.get("cate_pa");
                long created = (long) dbObject.get("created");
                
                newsCateData = new NewsCateData(id, name, created, id_cate_pa, code);
            }
        } catch(Exception ex) {
            logger.error(ex.getMessage(), ex);
            return null;
        }
        return newsCateData;
    }
    
    
    public String getNewsCateList() {        
        String lstNewsCate = "";
        try {
            BasicDBObject basicDBObject = new BasicDBObject("name", 1);
            DB db = MongoDbFactory.getConnection();
            DBCollection coll = db.getCollection(NEWSCATE_COLLECTION);
            DBCursor cursor = coll.find().sort(basicDBObject);    
            
            while (cursor.hasNext()) {
                DBObject dbObject = cursor.next();
                String objectId = ((ObjectId) dbObject.get("_id")).toString();
                String name = (String) dbObject.get("name");     
                
                lstNewsCate += "{label: \"" + name + "\", value: \"" + objectId + "\"},";                                
            }
        } catch (Exception ex) {
            logger.error("NewsCateModel.getNewsCateList: " + ex.getMessage(), ex);
        }
        return lstNewsCate.substring(0, lstNewsCate.length() - 1);
    }
    
}
